﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameLib.WorldMap;
using Sandbox.Serialization.XmlSerializers;

namespace Sandbox.Serialization.StringSerializers
{
	public class TileLocSerializer : StringSerializer<Tile>
	{
		public override string ConvertToStringTyped(Tile obj, GameLib.Serialization.DataFlags dataFlags)
		{
			if (obj == null)
				return NullString;
			return string.Format("{0},{1}", obj.X, obj.Y);
		}

		protected override object FromString(Type type, string str, GameLib.Serialization.DataFlags dataFlags)
		{
			if (str == NullString)
				return null;

			string[] coords = str.Split(',');

			if (coords.Length != 2)
				throw new Exception("Tile coordinate stirng is in incorrect format: " + str);

			int x = Convert.ToInt32(coords[0]);
			int y = Convert.ToInt32(coords[1]);

			return CurrentMapSerializer.GetTile(x, y);
		}

		public MapSerializer CurrentMapSerializer
		{
			get { return _CurrentMapSerializer; }
			set
			{
				if (_CurrentMapSerializer != null && value != null)
					throw new Exception("The TileLocSerializer has been working under scope of " + _CurrentMapSerializer.ToString() + ", so it cannot be assigned to other scope:" + value.ToString());
				_CurrentMapSerializer = value;
			}
		}
		public MapSerializer _CurrentMapSerializer = null;
	}
}
